//
//  HGPluginUtil.m
//  HGMM
//
//  Created by 2008-bj5 on 15/8/8.
//  Copyright (c) 2015年 cc. All rights reserved.
//

#import "HGPluginUtil.h"
#import "CCLog.h"
//#import "HGRequestUtil.h"

#define PLUGIN_DIR  @"_plugin"
#define PLUGIN_DATA_DIR  @"_plugin_data"

@implementation HGPluginUtil

- (NSString *)rootDir{
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *root = [paths lastObject];
    
    return [NSString stringWithFormat:@"%@/%@/%@", root, GUID_MMT, PLUGIN_DIR];
}

- (NSString *)rootDataDir{
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *root = [paths lastObject];
    
    return [NSString stringWithFormat:@"%@/%@", root, PLUGIN_DATA_DIR];
}

- (NSString *)getPluginDataDir:(NSString *)pluginId{
    NSString *root = [self rootDataDir];
    
    NSString *dir = [NSString stringWithFormat:@"%@/%@/%@", root, GUID_MMT, pluginId];
    
    return dir;
}

- (NSString *)getPluginIndexPath:(NSString *)pluginId{
    return [NSString stringWithFormat:@"%@/index.html",[self getPluginDir:pluginId]];
}

- (NSString *)getPluginLogo:(NSString *)pluginId{
    return [NSString stringWithFormat:@"%@/logo.png",[self getPluginDir:pluginId]];
}

- (BOOL)isExistedPluginDir:(NSString *)pluginId{
    NSString *dir = [self getPluginDir:pluginId];
    BOOL isDir = NO;
    NSFileManager *fm = [NSFileManager defaultManager];
    BOOL existed = [fm fileExistsAtPath:dir isDirectory:&isDir];
    
    return isDir && existed;
}

- (NSString *)getPluginDir:(NSString *)pluginId{
    NSString *root = [self rootDir];
    
    NSString *dir = [NSString stringWithFormat:@"%@/%@", root, pluginId];
    
    return dir;
}

- (BOOL)createPluginDir:(NSString *)pluginId{
    NSString *dir = [self getPluginDir:pluginId];
    BOOL isDir = NO;
    NSFileManager *fm = [NSFileManager defaultManager];
    BOOL existed = [fm fileExistsAtPath:dir isDirectory:&isDir];
    
    if (!isDir && existed) {
        [self deletePluginDir:pluginId];
    }
    
    if ( !(isDir && existed ) ) {
        NSError *err = nil;
        BOOL isOK = [fm createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:nil error:&err];
        if (err) {
            CCLog(@"createPluginDir [ERROR]: %@", err);
        }
        
        if (isOK && !err) {
            CCLog(@"PluginDir [%@]: %@", pluginId, dir);
        }
        
        return isOK;
    }
    return YES;
}

- (BOOL)deletePluginDir:(NSString *)pluginId{
    NSString *dir = [self getPluginDir:pluginId];
    NSFileManager *fm = [NSFileManager defaultManager];
    
    NSError *err = nil;
    
    BOOL isOK = [fm removeItemAtPath:dir error:&err];
    
    if (err) {
        CCLog(@"deletePluginDir [ERROR]: %@", err);
    }
    return isOK;
}

+ (void)clearCache{
    
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }
    
}

- (void)clearAllPluginData{
    NSString *dir = [self rootDataDir];
    NSFileManager *fm = [NSFileManager defaultManager];
    
    NSError *err = nil;
    
    BOOL isOK = [fm removeItemAtPath:dir error:&err];
    
    if (err) {
        CCLog(@"clearAllPluginData [ERROR]: %@", err);
    }
    
    CCLog(@"clearAllPluginData [isOK]: %d", isOK);
}

- (BOOL)clearPluginData:(NSString *)pluginId{
    NSString *dir = [self getPluginDataDir:pluginId];
    NSFileManager *fm = [NSFileManager defaultManager];
    
    NSError *err = nil;
    
    BOOL isOK = [fm removeItemAtPath:dir error:&err];
    
    if (err) {
        CCLog(@"clearPluginData [ERROR]: %@", err);
    }
    return isOK;
}

@end
